package appfight.importApp;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import appfight.icon.manager.IconManager;
import appfight.icon.manager.IconManager.PInfo;
import braindamagestudio.com.app.fight.R;

public class ImportAppMenu extends Activity {
	private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();
	private boolean isSelectAll = false;
	private int selectNum = 0;
	
	private void SetSelectNum(int num)
	{
		selectNum = num;
		TextView text = (TextView)this.findViewById(R.id.Textview1);
		text.setText("Please select more than ten app~  \nCurrent Select : " + selectNum); 
	}
	
	private int GetSelectNum()
	{
		return selectNum;
	}

	private List<PInfo> allApp;
	private IconManager iconManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        setContentView(R.layout.importappmenu);
        
        LinearLayout v = (LinearLayout)this.findViewById(R.id.mainLinear);
        if(v == null)finish();
        
        iconManager = new IconManager(this);
        allApp = iconManager.getInstalledApps(false);
        
        for(int i=0;i<allApp.size();i++)
        {

            LinearLayout tempview = new LinearLayout(this);
            
        	CheckBox c = new CheckBox(this);
        	c.setOnCheckedChangeListener(new OnCheckedChangeListener()
        	{
        	    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        	    {
        	        if ( isChecked )
        	        {
        	        	SetSelectNum(GetSelectNum() + 1);
        	        }else
        	        {

        	        	SetSelectNum(GetSelectNum() - 1);
        	        }

        	    }
        	});
        	c.setText(allApp.get(i).appname);
        	
        	checkBoxList.add(c);
        	
        	ImageView imageView = new ImageView(this);
        	imageView.setImageDrawable(allApp.get(i).icon);
        	imageView.setLayoutParams(new GridView.LayoutParams(32, 32));
        	tempview.addView(imageView);
        	tempview.addView(c);
        	
        	v.addView(tempview);
        	
        }
    }
    
    public void SelectAll(View v)
    {
    	for(int i=0;i<checkBoxList.size();i++)
    	{
    		checkBoxList.get(i).setChecked(!isSelectAll);
    	}
    	isSelectAll = !isSelectAll;
    	if(isSelectAll)
    	{
    		this.SetSelectNum(checkBoxList.size());
    	}else
    	{
    		this.SetSelectNum(0);
    	}
    	
    }
    
    public void OKButtonClick(View v)
    {
    	ArrayList<PInfo> importApp = new ArrayList<PInfo>();

    	for(int i=0;i<checkBoxList.size();i++)
    	{
    		if(checkBoxList.get(i).isChecked())
    		{
    			importApp.add(allApp.get(i));
    		}
    	}
    	Log.d("test","saveIcon"+ importApp.size());
    	iconManager.saveIcons(importApp);
    	finish();
    }
}
